<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Admin\Controller\AdminController;
class NavigationController extends AdminController {
    function _initialize() {
        parent::_initialize();
        $this->assign('action', 'navigation');
        $this->assign('sys', 'website'); // 数据集
    }
    //put your code here
    public function index(){
        $navi = M('navigation');
        $count = $navi->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $list = $navi->order('nav_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('pages',$show);// 赋值分页输出
        $this->display();
    }
    
    
    
    public function  add(){
        $tree = new \Libs\Tree\Tree();
        $category = M("Category")->select();
        $arr = $tree->channelLevel($category, '0', '&nbsp;', 'gc_id', 'gc_parent_id');
        $this->assign("arr", $arr);
        $article_cat = M("article_cat")->select();
        $array = $tree->channelLevel($article_cat, '0', '&nbsp;', 'cat_id', 'parent_id');
        $this->assign("array", $array);
        if(IS_POST){
            $datas = I('post.');
            switch($datas['nav_type']){
                case 0 ;$datas['item_id'] = $datas['nav_type']; break;
                case 1 ;$datas['item_id'] = $datas['goods_class_id']; break;
                case 2 ;$datas['item_id'] = $datas['article_class_id']; break;
                case 3 ;$datas['item_id'] = $datas['activity_id']; break;
            }
            $navi = M('navigation');
            $res = $navi->data($datas)->add();

            $this->cache(I('nav_location'));

            if($res){
                $this->success('新增成功',U('index'));
            }else{
                $this->error('新增失败');
            }
        }

        $this->display();
    }


    public function edit(){
        $tree = new \Libs\Tree\Tree();
        $category = M("Category")->select();
        $arr = $tree->channelLevel($category, '0', '&nbsp;', 'gc_id', 'gc_parent_id');
        $this->assign("arr", $arr);
        $article_cat = M("article_cat")->select();
        $array = $tree->channelLevel($article_cat, '0', '&nbsp;', 'cat_id', 'parent_id');
        $this->assign("array", $array);
        $nav_id['nav_id'] = I('get.nav_id');
        $navi = M('navigation');
        $res = $navi->where($nav_id)->find();
        $this->assign('res',$res);
        switch($res['nav_type']){
            case 0 ;$item_id[0] = "checked='checked'" ; break;
            case 1 ;$item_id[1] = "checked='checked'"; break;
            case 2 ;$item_id[2] = "checked='checked'"; break;
            case 3 ;$item_id[3] = "checked='checked'"; break;
        }
        $this->assign('item_id',$item_id);
        switch($res['nav_location']){
            case 0 ;$nav_location[0] = "checked='checked'"; break;
            case 1 ;$nav_location[1] = "checked='checked'"; break;
            case 2 ;$nav_location[2] = "checked='checked'"; break;
            case 3 ;$nav_location[3] = "checked='checked'"; break;
            case 4 ;$nav_location[4] = "checked='checked'"; break;
        }
        $this->assign('nav_location',$nav_location);
        if(IS_POST){
            $datas = I('post.');
            switch($datas['nav_type']){
                case 0 ;$datas['item_id'] = $datas['nav_type']; break;
                case 1 ;$datas['item_id'] = $datas['goods_class_id']; break;
                case 2 ;$datas['item_id'] = $datas['article_class_id']; break;
                case 3 ;$datas['item_id'] = $datas['activity_id']; break;
            }
            $navi->where($nav_id)->save($datas);

            $this->cache(I('nav_location'));

            $this->success('操作成功',U('index'));
        }
        
        $this->display();
    }
    
    
    public function del(){
        $nav_id['nav_id'] = I('get.nav_id');
        $navi = M('navigation');
        $nav_location = $navi->where($nav_id)->field('nav_location')->find();

        $res = $navi->where($nav_id)->delete();

        $this->cache($nav_location['nav_location']);

        if($res !== FALSE){
            $this->success('操作成功',U('index'));
        }else{
            $this->error('操作失败');
        }
    }

    //向缓存中添加 $nav_location 更新的位置
    public function cache($nav_location){
        $a = M('navigation')->where(array('nav_location'=>$nav_location))->select();
        switch(I('nav_location')){
            case 0:bareak;
            case 1:
                S('middle_navigation',$a);
                bareak;
            case 2:bareak;
            case 3:bareak;
            case 4:bareak;
        }
    }

}
